package com.lyt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyt.common.Result;
import com.lyt.entity.Goods;
import com.lyt.entity.MonthlyData;
import com.lyt.entity.Record;
import com.lyt.entity.Storage;
import com.lyt.entity.User;
import com.lyt.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijinxuan
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/monthlyData")
public class MonthlyDataController {

    @Resource
    private MonthlyDataService monthlyDataService;

    @Resource
    private UserService userService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private StorageService storageService;

    @Resource
    private RecordService recordService;

    private static final Logger LOG = LoggerFactory.getLogger(MonthlyDataController.class);

    //获得vehicles表里面的指定车辆数据，每星期的租车数据和天气情况。
    @GetMapping("/list")
    public Result getList() {
        //查询物品种类数量
        int goods = goodsService.list().size();

        //再查个用户数据
        int users = userService.list().size();

        //查询仓库数量
        int storages = storageService.list().size();

        //查询记录数量
        int records = recordService.list().size();

        //从月份里面抽取数据
        LocalDateTime currentTime = LocalDateTime.now();
        int currentYear = currentTime.getYear();

        LambdaQueryWrapper<MonthlyData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MonthlyData::getYear, currentYear);

        // 查询当年的 MonthlyData
        if (monthlyDataService.getOne(queryWrapper) == null) {
            MonthlyData monthlyData = new MonthlyData();
            monthlyData.setYear(currentYear);
            monthlyDataService.saveOrUpdate(monthlyData);
        }

        MonthlyData monthlyData = monthlyDataService.getOne(queryWrapper);

        //status状态集合
        HashMap<String, Integer> result = new HashMap<>();
        result.put("goods", goods);
        result.put("records", records);
        result.put("storages", storages);
        result.put("users", users);
        result.put("January", monthlyData.getJanuary());
        result.put("February", monthlyData.getFebruary());
        result.put("March", monthlyData.getMarch());
        result.put("April", monthlyData.getApril());
        result.put("May", monthlyData.getMay());
        result.put("June", monthlyData.getJune());
        result.put("July", monthlyData.getJuly());
        result.put("August", monthlyData.getAugust());
        result.put("September", monthlyData.getSeptember());
        result.put("October", monthlyData.getOctober());
        result.put("November", monthlyData.getNovember());
        result.put("December", monthlyData.getDecember());

        return Result.suc(result, (long) result.size());
    }


}
